package com.tanhua.recommend.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.VideoScore;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-video",consumerGroup = "tanhua-video-group")
public class VideoScoreListener implements RocketMQListener<String> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(String s) {
        try {
            Map<String,Object> map = JSON.parseObject(s, Map.class);

            String videoId = (String) map.get("videoId");
            Long userId = (Long) map.get("userId");
            Integer type = (Integer) map.get("type");
            Long vid = (Long) map.get("vid");

            VideoScore score = new VideoScore();
            score.setUserId(userId);
            score.setVideoId(vid);
            score.setDate(System.currentTimeMillis());

            switch (type){
                case 1: { //发布视频
                    score.setScore(2d);
                    break;
                }
                case 2: { //视频点赞
                    score.setScore(5d);
                    break;
                }
                case 3: { //取消点赞
                    score.setScore(-5d);
                    break;
                }
                case 4: { //发布评论
                    score.setScore(10d);
                    break;
                }
                default: {
                    score.setScore(0d);
                    break;
                }
            }
            mongoTemplate.save(score);
        } catch (Exception e) {
            log.error("消费视频消息出错，错误信息：", e);
        }
    }
}
